package com.jshoperxms.service.impl;

import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.dao.SysRoleModuleActionRpTDao;
import com.jshoperxms.dao.SysRoleTDao;
import com.jshoperxms.entity.SysRoleModuleActionRpT;
import com.jshoperxms.entity.SysRoleT;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.jshoperxms.service.SysRoleTService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("sysRoleTService")
@Scope("prototype")
public class SysRoleTServiceImpl extends BaseTServiceImpl<SysRoleT>implements SysRoleTService {

    @Resource
    private SysRoleTDao sysRoleTDao;

    @Resource
    private SysRoleModuleActionRpTDao sysRoleModuleActionRpTDao;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void delSysRoleTransactional(String[] ids) {
        for(String s:ids){
            SysRoleT sysRoleT=this.sysRoleTDao.findByPK(SysRoleT.class,s);
            if(sysRoleT!=null){
                sysRoleT.setStatus(BaseEnums.DataUsingState.DEL.getState());
                sysRoleT.setUpdatetime(DateTools.getSystemTimeBi());
                sysRoleT.setVersiont(sysRoleT.getVersiont()+1);
                this.sysRoleTDao.update(sysRoleT);
                Criterion criterion= Restrictions.eq("sysroleid",sysRoleT.getId());
                List<SysRoleModuleActionRpT> list=this.sysRoleModuleActionRpTDao.findByCriteria(SysRoleModuleActionRpT.class,criterion);
                if(!list.isEmpty()){
                    for(SysRoleModuleActionRpT sys:list){
                        sys.setUpdatetime(DateTools.getSystemTimeBi());
                        sys.setVersiont(sys.getVersiont()+1);
                        sys.setStatus(BaseEnums.DataUsingState.DEL.getState());
                        this.sysRoleModuleActionRpTDao.update(sys);
                    }
                }
            }
        }
    }

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void updateSysRoleTransactional(SysRoleT sysRoleT) {
        SysRoleT bean=this.sysRoleTDao.findByPK(SysRoleT.class,sysRoleT.getId());
        if(bean!=null){
            bean.setSysrolename(sysRoleT.getSysrolename());
            bean.setNote(sysRoleT.getNote());
            bean.setStatus(sysRoleT.getStatus());
            bean.setUpdatetime(DateTools.getSystemTimeBi());
            bean.setVersiont(bean.getVersiont()+1);
            this.sysRoleTDao.update(bean);
            Criterion criterion= Restrictions.eq("sysroleid",sysRoleT.getId());
            List<SysRoleModuleActionRpT> list=this.sysRoleModuleActionRpTDao.findByCriteria(SysRoleModuleActionRpT.class,criterion);
            if(!list.isEmpty()){
                for(SysRoleModuleActionRpT sys:list){
                    sys.setUpdatetime(DateTools.getSystemTimeBi());
                    sys.setVersiont(sys.getVersiont()+1);
                    sys.setStatus(BaseEnums.DataUsingState.DEL.getState());
                    sys.setSysroleid(sysRoleT.getId());
                    sys.setSysrolename(sysRoleT.getSysrolename());
                    this.sysRoleModuleActionRpTDao.update(sys);
                }
            }
        }

    }
}
